package smartterracedfieldsolution.smartteracedfieldsolution.service.controller;


import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import smartterracedfieldsolution.smartteracedfieldsolution.common.result.Result;
import smartterracedfieldsolution.smartteracedfieldsolution.pojo.dto.RegisterRequest;
import smartterracedfieldsolution.smartteracedfieldsolution.pojo.entity.User;
import smartterracedfieldsolution.smartteracedfieldsolution.service.service.UserService;

import java.time.LocalDateTime;

/*
    登录控制
 */

@RequestMapping("/api/auth")
@RestController
@Tag(name = "login")
public class AuthController {

    private final UserService userService;

    @Autowired
    public AuthController(UserService userService) {
        this.userService = userService;
    }

    @Operation(summary = "测试Swagger3注解方法Post")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "请求成功"),
            @ApiResponse(responseCode = "400", description = "请求参数没填好"),
            @ApiResponse(responseCode = "401", description = "没有权限"),
            @ApiResponse(responseCode = "403", description = "禁止访问"),
            @ApiResponse(responseCode = "404", description = "请求路径没有或页面跳转路径不对")
    })
    @PostMapping("/login")
    public Result<User> login(@RequestParam("user_name") String username, @RequestParam("password") String password) {
        try{
            User user = userService.login(username, password);
            return Result.success(user,"登录成功");
        }catch (RuntimeException e){
            return Result.fail(400,e.getMessage());
        }
    }

    @Operation(summary = "用户注册")
    @PostMapping("/register")
    public Result<Boolean> register(@Valid @RequestBody RegisterRequest request){
        return userService.register(request);
    }
}
